{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "import QUANTAXIS as QA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sys\n",
    "\n",
    "if QA.__version__=='1.2.0.dev3':\n",
    "    pass\n",
    "else:\n",
    "    print('升级QA pip install QUANTAXIS==1.2.0.dev3')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "在这个示例中:\n",
    "\n",
    "1. 测试了简单的开仓平仓\n",
    "2. 测试了连续方向开仓\n",
    "3. 测试的多空同时开仓, 以及合并平仓等行为\n",
    "\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "Account=QA.QA_Account(init_cash=10000,allow_sellopen=True,allow_t0=True,account_cookie='future_test',market_type=QA.MARKET_TYPE.FUTURE_CN,frequence=QA.FREQUENCE.FIVE_MIN)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "Broker=QA.QA_BacktestBroker()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "Account.receive_simpledeal?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n简易测试\\n'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "简易测试\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "Account.receive_simpledeal('RBL8',3800,1, QA.ORDER_DIRECTION.BUY_OPEN,'2018-11-09 9:25:00','多开')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'RBL8': {2: {'money': 3800.95, 'amount': 1}, -2: {'money': 0, 'amount': 0}}}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#冻结的保证金 : 3800+ 手续费\n",
    "Account.frozen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6199.05"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 实际现金\n",
    "Account.cash_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "RBL8    1\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 持仓 (1手多单)\n",
    "Account.sell_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 平仓"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "Account.receive_simpledeal('RBL8',3820,1, QA.ORDER_DIRECTION.SELL_CLOSE,'2018-11-09 9:30:00','多平')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'RBL8': {2: {'money': 0, 'amount': 0}, -2: {'money': 0, 'amount': 0}}}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#冻结的保证金 : 0  释放\n",
    "Account.frozen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10018.095000000001"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 实际现金\n",
    "Account.cash_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "RBL8    0\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 持仓 (1手多单)\n",
    "Account.sell_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>code</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>cash</th>\n",
       "      <th>order_id</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>commission</th>\n",
       "      <th>tax</th>\n",
       "      <th>message</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-11-09 9:25:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3800</td>\n",
       "      <td>1</td>\n",
       "      <td>6199.050</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.950</td>\n",
       "      <td>0</td>\n",
       "      <td>多开</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-11-09 9:30:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3820</td>\n",
       "      <td>-1</td>\n",
       "      <td>10018.095</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.955</td>\n",
       "      <td>0</td>\n",
       "      <td>多平</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             datetime  code  price  amount       cash order_id realorder_id  \\\n",
       "0  2018-11-09 9:25:00  RBL8   3800       1   6199.050     None         None   \n",
       "1  2018-11-09 9:30:00  RBL8   3820      -1  10018.095     None         None   \n",
       "\n",
       "  trade_id account_cookie  commission  tax message  \n",
       "0     None    future_test       0.950    0      多开  \n",
       "1     None    future_test       0.955    0      多平  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account.history_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n连续开平仓\\n'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "连续开平仓\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "Account1=QA.QA_Account(init_cash=100000,allow_sellopen=True,allow_t0=True,account_cookie='future_test',market_type=QA.MARKET_TYPE.FUTURE_CN,frequence=QA.FREQUENCE.FIVE_MIN)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "Account1.receive_simpledeal('RBL8',3800,1, QA.ORDER_DIRECTION.BUY_OPEN,'2018-11-09 9:25:00','多开')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "Account1.receive_simpledeal('RBL8',3750,1, QA.ORDER_DIRECTION.BUY_OPEN,'2018-11-09 9:30:00','多开')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "Account1.receive_simpledeal('RBL8',3720,1, QA.ORDER_DIRECTION.BUY_OPEN,'2018-11-09 9:35:00','多开')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'RBL8': {2: {'money': 3757.605833333333, 'amount': 3},\n",
       "  -2: {'money': 0, 'amount': 0}}}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account1.frozen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>code</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>cash</th>\n",
       "      <th>order_id</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>commission</th>\n",
       "      <th>tax</th>\n",
       "      <th>message</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-11-09 9:25:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3800</td>\n",
       "      <td>1</td>\n",
       "      <td>96199.0500</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9500</td>\n",
       "      <td>0</td>\n",
       "      <td>多开</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-11-09 9:30:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3750</td>\n",
       "      <td>1</td>\n",
       "      <td>92448.1125</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9375</td>\n",
       "      <td>0</td>\n",
       "      <td>多开</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-11-09 9:35:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3720</td>\n",
       "      <td>1</td>\n",
       "      <td>88727.1825</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9300</td>\n",
       "      <td>0</td>\n",
       "      <td>多开</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             datetime  code  price  amount        cash order_id realorder_id  \\\n",
       "0  2018-11-09 9:25:00  RBL8   3800       1  96199.0500     None         None   \n",
       "1  2018-11-09 9:30:00  RBL8   3750       1  92448.1125     None         None   \n",
       "2  2018-11-09 9:35:00  RBL8   3720       1  88727.1825     None         None   \n",
       "\n",
       "  trade_id account_cookie  commission  tax message  \n",
       "0     None    future_test      0.9500    0      多开  \n",
       "1     None    future_test      0.9375    0      多开  \n",
       "2     None    future_test      0.9300    0      多开  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account1.history_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "Account1.receive_simpledeal('RBL8',3780,1, QA.ORDER_DIRECTION.SELL_CLOSE,'2018-11-09 13:30:00','多平')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'RBL8': {2: {'money': 3757.605833333333, 'amount': 2},\n",
       "  -2: {'money': 0, 'amount': 0}}}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account1.frozen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "92506.2375"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account1.cash_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "Account1.receive_simpledeal('RBL8',3738,1, QA.ORDER_DIRECTION.SELL_CLOSE,'2018-11-09 13:35:00','多平')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'RBL8': {2: {'money': 3757.605833333333, 'amount': 1},\n",
       "  -2: {'money': 0, 'amount': 0}}}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account1.frozen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "96243.303"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account1.cash_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "Account1.receive_simpledeal('RBL8',3820,1, QA.ORDER_DIRECTION.SELL_CLOSE,'2018-11-09 14:35:00','多平')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100062.348"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account1.cash_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>code</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>cash</th>\n",
       "      <th>order_id</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>commission</th>\n",
       "      <th>tax</th>\n",
       "      <th>message</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-11-09 9:25:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3800</td>\n",
       "      <td>1</td>\n",
       "      <td>96199.0500</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9500</td>\n",
       "      <td>0</td>\n",
       "      <td>多开</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-11-09 9:30:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3750</td>\n",
       "      <td>1</td>\n",
       "      <td>92448.1125</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9375</td>\n",
       "      <td>0</td>\n",
       "      <td>多开</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-11-09 9:35:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3720</td>\n",
       "      <td>1</td>\n",
       "      <td>88727.1825</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9300</td>\n",
       "      <td>0</td>\n",
       "      <td>多开</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-11-09 13:30:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3780</td>\n",
       "      <td>-1</td>\n",
       "      <td>92506.2375</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9450</td>\n",
       "      <td>0</td>\n",
       "      <td>多平</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-11-09 13:35:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3738</td>\n",
       "      <td>-1</td>\n",
       "      <td>96243.3030</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9345</td>\n",
       "      <td>0</td>\n",
       "      <td>多平</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2018-11-09 14:35:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3820</td>\n",
       "      <td>-1</td>\n",
       "      <td>100062.3480</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9550</td>\n",
       "      <td>0</td>\n",
       "      <td>多平</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              datetime  code  price  amount         cash order_id  \\\n",
       "0   2018-11-09 9:25:00  RBL8   3800       1   96199.0500     None   \n",
       "1   2018-11-09 9:30:00  RBL8   3750       1   92448.1125     None   \n",
       "2   2018-11-09 9:35:00  RBL8   3720       1   88727.1825     None   \n",
       "3  2018-11-09 13:30:00  RBL8   3780      -1   92506.2375     None   \n",
       "4  2018-11-09 13:35:00  RBL8   3738      -1   96243.3030     None   \n",
       "5  2018-11-09 14:35:00  RBL8   3820      -1  100062.3480     None   \n",
       "\n",
       "  realorder_id trade_id account_cookie  commission  tax message  \n",
       "0         None     None    future_test      0.9500    0      多开  \n",
       "1         None     None    future_test      0.9375    0      多开  \n",
       "2         None     None    future_test      0.9300    0      多开  \n",
       "3         None     None    future_test      0.9450    0      多平  \n",
       "4         None     None    future_test      0.9345    0      多平  \n",
       "5         None     None    future_test      0.9550    0      多平  "
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account1.history_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n双开测试 \\n\\n'"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "双开测试 \n",
    "\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "Account2=QA.QA_Account(init_cash=100000,allow_sellopen=True,allow_t0=True,account_cookie='future_test',market_type=QA.MARKET_TYPE.FUTURE_CN,frequence=QA.FREQUENCE.FIVE_MIN)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "Account2.receive_simpledeal('RBL8',3800,1, QA.ORDER_DIRECTION.BUY_OPEN,'2018-11-09 9:25:00','多开')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "Account2.receive_simpledeal('RBL8',3810,1, QA.ORDER_DIRECTION.SELL_OPEN,'2018-11-09 9:30:00','空开')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>code</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>cash</th>\n",
       "      <th>order_id</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>commission</th>\n",
       "      <th>tax</th>\n",
       "      <th>message</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-11-09 9:25:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3800</td>\n",
       "      <td>1</td>\n",
       "      <td>96199.0500</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9500</td>\n",
       "      <td>0</td>\n",
       "      <td>多开</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-11-09 9:30:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3810</td>\n",
       "      <td>-1</td>\n",
       "      <td>92390.0025</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9525</td>\n",
       "      <td>0</td>\n",
       "      <td>空开</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             datetime  code  price  amount        cash order_id realorder_id  \\\n",
       "0  2018-11-09 9:25:00  RBL8   3800       1  96199.0500     None         None   \n",
       "1  2018-11-09 9:30:00  RBL8   3810      -1  92390.0025     None         None   \n",
       "\n",
       "  trade_id account_cookie  commission  tax message  \n",
       "0     None    future_test      0.9500    0      多开  \n",
       "1     None    future_test      0.9525    0      空开  "
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account2.history_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'RBL8': {2: {'money': 3800.95, 'amount': 1},\n",
       "  -2: {'money': 3809.0475, 'amount': 1}}}"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account2.frozen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "92390.0025"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account2.cash_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "Account2.receive_simpledeal('RBL8',3750,1, QA.ORDER_DIRECTION.BUY_OPEN,'2018-11-09 11:25:00','多开')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>code</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>cash</th>\n",
       "      <th>order_id</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>commission</th>\n",
       "      <th>tax</th>\n",
       "      <th>message</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-11-09 9:25:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3800</td>\n",
       "      <td>1</td>\n",
       "      <td>96199.0500</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9500</td>\n",
       "      <td>0</td>\n",
       "      <td>多开</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-11-09 9:30:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3810</td>\n",
       "      <td>-1</td>\n",
       "      <td>92390.0025</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9525</td>\n",
       "      <td>0</td>\n",
       "      <td>空开</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-11-09 11:25:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3750</td>\n",
       "      <td>1</td>\n",
       "      <td>88639.0650</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9375</td>\n",
       "      <td>0</td>\n",
       "      <td>多开</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              datetime  code  price  amount        cash order_id realorder_id  \\\n",
       "0   2018-11-09 9:25:00  RBL8   3800       1  96199.0500     None         None   \n",
       "1   2018-11-09 9:30:00  RBL8   3810      -1  92390.0025     None         None   \n",
       "2  2018-11-09 11:25:00  RBL8   3750       1  88639.0650     None         None   \n",
       "\n",
       "  trade_id account_cookie  commission  tax message  \n",
       "0     None    future_test      0.9500    0      多开  \n",
       "1     None    future_test      0.9525    0      空开  \n",
       "2     None    future_test      0.9375    0      多开  "
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account2.history_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'RBL8': {2: {'money': 3775.94375, 'amount': 2},\n",
       "  -2: {'money': 3809.0475, 'amount': 1}}}"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account2.frozen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "88639.065"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account2.cash_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "Account2.receive_simpledeal('RBL8',3800,2, QA.ORDER_DIRECTION.SELL_CLOSE,'2018-11-09 14:35:00','多平')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'RBL8': {2: {'money': 0, 'amount': 0}, -2: {'money': 3809.0475, 'amount': 1}}}"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account2.frozen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>code</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>cash</th>\n",
       "      <th>order_id</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>commission</th>\n",
       "      <th>tax</th>\n",
       "      <th>message</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-11-09 9:25:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3800</td>\n",
       "      <td>1</td>\n",
       "      <td>96199.0500</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9500</td>\n",
       "      <td>0</td>\n",
       "      <td>多开</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-11-09 9:30:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3810</td>\n",
       "      <td>-1</td>\n",
       "      <td>92390.0025</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9525</td>\n",
       "      <td>0</td>\n",
       "      <td>空开</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-11-09 11:25:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3750</td>\n",
       "      <td>1</td>\n",
       "      <td>88639.0650</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9375</td>\n",
       "      <td>0</td>\n",
       "      <td>多开</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-11-09 14:35:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3800</td>\n",
       "      <td>-2</td>\n",
       "      <td>96237.1650</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>1.9000</td>\n",
       "      <td>0</td>\n",
       "      <td>多平</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              datetime  code  price  amount        cash order_id realorder_id  \\\n",
       "0   2018-11-09 9:25:00  RBL8   3800       1  96199.0500     None         None   \n",
       "1   2018-11-09 9:30:00  RBL8   3810      -1  92390.0025     None         None   \n",
       "2  2018-11-09 11:25:00  RBL8   3750       1  88639.0650     None         None   \n",
       "3  2018-11-09 14:35:00  RBL8   3800      -2  96237.1650     None         None   \n",
       "\n",
       "  trade_id account_cookie  commission  tax message  \n",
       "0     None    future_test      0.9500    0      多开  \n",
       "1     None    future_test      0.9525    0      空开  \n",
       "2     None    future_test      0.9375    0      多开  \n",
       "3     None    future_test      1.9000    0      多平  "
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account2.history_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "96237.16500000001"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account2.cash_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "Account2.receive_simpledeal('RBL8',3750,1, QA.ORDER_DIRECTION.BUY_CLOSE,'2018-11-09 14:50:00','空平')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>code</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>cash</th>\n",
       "      <th>order_id</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>commission</th>\n",
       "      <th>tax</th>\n",
       "      <th>message</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-11-09 9:25:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3800</td>\n",
       "      <td>1</td>\n",
       "      <td>96199.0500</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9500</td>\n",
       "      <td>0</td>\n",
       "      <td>多开</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-11-09 9:30:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3810</td>\n",
       "      <td>-1</td>\n",
       "      <td>92390.0025</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9525</td>\n",
       "      <td>0</td>\n",
       "      <td>空开</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-11-09 11:25:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3750</td>\n",
       "      <td>1</td>\n",
       "      <td>88639.0650</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9375</td>\n",
       "      <td>0</td>\n",
       "      <td>多开</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-11-09 14:35:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3800</td>\n",
       "      <td>-2</td>\n",
       "      <td>96237.1650</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>1.9000</td>\n",
       "      <td>0</td>\n",
       "      <td>多平</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-11-09 14:50:00</td>\n",
       "      <td>RBL8</td>\n",
       "      <td>3750</td>\n",
       "      <td>1</td>\n",
       "      <td>100104.3225</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>0.9375</td>\n",
       "      <td>0</td>\n",
       "      <td>空平</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              datetime  code  price  amount         cash order_id  \\\n",
       "0   2018-11-09 9:25:00  RBL8   3800       1   96199.0500     None   \n",
       "1   2018-11-09 9:30:00  RBL8   3810      -1   92390.0025     None   \n",
       "2  2018-11-09 11:25:00  RBL8   3750       1   88639.0650     None   \n",
       "3  2018-11-09 14:35:00  RBL8   3800      -2   96237.1650     None   \n",
       "4  2018-11-09 14:50:00  RBL8   3750       1  100104.3225     None   \n",
       "\n",
       "  realorder_id trade_id account_cookie  commission  tax message  \n",
       "0         None     None    future_test      0.9500    0      多开  \n",
       "1         None     None    future_test      0.9525    0      空开  \n",
       "2         None     None    future_test      0.9375    0      多开  \n",
       "3         None     None    future_test      1.9000    0      多平  \n",
       "4         None     None    future_test      0.9375    0      空平  "
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account2.history_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'RBL8': {2: {'money': 0, 'amount': 0}, -2: {'money': 0, 'amount': 0}}}"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account2.frozen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100104.32250000001"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account2.cash_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
